#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

struct TreeNode {
    int key;
    TreeNode *parent = nullptr;
    vector<TreeNode *> children;
};

void searchChildren(TreeNode *node, TreeNode *exclude, int depth, int &maxDepth) {
    for (int i = 0; i < node->children.size(); ++i) {
        if (exclude == node->children[i]) {
            continue;
        }
        searchChildren(node->children[i], nullptr, depth + 1, maxDepth);
    }
    maxDepth = max(maxDepth, depth);
}

void searchParent(TreeNode *node, int depth, int &maxCount) {
    if (!node->parent) {
        return;
    }
    searchParent(node->parent, depth + 1, maxCount);
    int d = 0;
    searchChildren(node->parent, node, 0, d);
    maxCount = max(maxCount, depth + d);
}

int main() {
    int n;
    cin >> n;
    vector<TreeNode *> tree(n);
    for (int i = 0; i < n; ++i) {
        TreeNode *node = new TreeNode;
        node->key = i + 1;
        tree[i] = node;
    }
    for (int i = 1; i < n; ++i) {
        int p;
        cin >> p;
        auto child = tree[i];
        auto parent = tree[p - 1];
        parent->children.push_back(child);
        child->parent = parent;
    }

    int x, y;
    cin >> x >> y;
    auto parent = tree[x - 1];
    auto child = tree[y - 1];

    int c1 = 0;
    int c2 = 0;
    int c3 = 0;
    searchParent(parent, 1, c1);
    searchChildren(parent, child, 0, c2);
    searchChildren(child, nullptr, 0, c3);

    cout << max(c1, c2) + c3 + 1 << endl;
    return 0;
}
